package interviewusa;

import java.util.Stack;

public class Main {
    Stack<Character> stack = new Stack<>();
    public static void main(String[] args) {
        Main m = new Main();
        System.out.println(m.isValid(new String("[]{}[()]([])")));
    }
    public boolean isValid(String s) {
        if (s.length() %2 != 0) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
                stack.push(s.charAt(i));
                continue;
            }
            if (s.charAt(i) == ')') {
                if (stack.isEmpty() || stack.pop() != '(') {
                    return false;
                }
            }
            if (s.charAt(i) == '}') {
                if (stack.isEmpty() || stack.pop() != '{') {
                    return false;
                }
            }
            if (s.charAt(i) == ']') {
                if (stack.isEmpty() || stack.pop() != '[') {
                    return false;
                }
            }
        }
        return true;
    }
}